package com.reciver.service.impl;

import java.util.List;
import com.reciver.bo.JcBo;
import com.reciver.bo.ZlBo;
import com.reciver.entity.Tbs;
import com.reciver.bo.ZlBo.Dt;
import com.reciver.bo.ZlBoSbwy;
import com.reciver.bo.ZlBo.DpBo;
import javax.annotation.Resource;
import com.reciver.mapper.TbsMapper;
import com.reciver.entity.TbsDmqxjcy;
import com.reciver.entity.TabAlarmInfo;
import com.reciver.common.util.DateUtil;
import com.reciver.mapper.TabDeviceMapper;
import com.reciver.mapper.TabOnlineMapper;
import com.reciver.mapper.TbsDmqxjcyMapper;
import com.reciver.mapper.TabAlarmInfoMapper;
import org.springframework.stereotype.Service;
import com.reciver.service.ReceiveDataService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lgb
 * @date 2020-9-11
 * @return 直连设备解析
 **/
@Service
public class ReceiveDataServiceImpl implements ReceiveDataService{
	
	@Resource
	private TbsMapper tbsMapper;
	@Resource
	private TabAlarmInfoMapper taiMapper;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private TabOnlineMapper tabOnlineMapper;
	@Resource
	private TabDeviceMapper tabDeviceMapper;
	@Resource
    private TbsDmqxjcyMapper tbsDmqxjcyMapper;
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
    public String parseZl(ZlBo zlBo) {
		List<DpBo> dpBoList = zlBo.getDp();
		for(DpBo dpBo : dpBoList) {
			Dt dbBo = dpBo.getDt();
			TbsDmqxjcy tbsDmqxjcy = new TbsDmqxjcy();
	        tbsDmqxjcy.setDate(DateUtil.formatTime(zlBo.getCt()));
	        tbsDmqxjcy.setDevId(zlBo.getCid());
	        tbsDmqxjcy.setNowX(dbBo.getSx());
	        tbsDmqxjcy.setNowY(dbBo.getSy());
	        tbsDmqxjcy.setNowZ(dbBo.getSz());
	        tbsDmqxjcy.setTp(dbBo.getTp());
	        tbsDmqxjcyMapper.insertSelective(tbsDmqxjcy);
		}
		/** 更新接收数据时间 */
		tabOnlineMapper.updateOnline(zlBo.getCid(),DateUtil.formatTime());
		/** 产生报警 */
		TabAlarmInfo info = new TabAlarmInfo();
		
		return "接收成功";
    }
	/**
	 * @date:2020-10-26
	 * @param jsonParam
	 * @return 解析深部位移
	 **/
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public void parseZlSbwy(ZlBoSbwy zlBoSbwy) {
		List<com.reciver.bo.ZlBoSbwy.DpBo> dpList = zlBoSbwy.getDp();
		String sensorNo = zlBoSbwy.getCid();
		String gpstime  = zlBoSbwy.getCt();
		for(com.reciver.bo.ZlBoSbwy.DpBo db : dpList) {
			if(db.getCn().equals("0")) {//0-设备信息包
				
			}else {
				String[] dtList = db.getDt();
				if(db.getSn()==dtList.length) {
					String value="";
					for(int i=0;i<dtList.length;i++) {
					    String dt = dtList[i];
						if(sensorNo.equals("sd20200904001") || sensorNo.equals("sd20200904002")||
								sensorNo.equals("sd20200907003")||sensorNo.equals("sd20200907004")) {
							if(i % 2 == 1) {
								value += dt+",";
							}
						} else {
							if(i % 3 == 1) {
								value += dt+",";
							}
						}
					}
					value = value.substring(0,value.length()-1);
					Tbs tbs = new Tbs();
					tbs.setSensorNo(sensorNo);
					tbs.setValue(value);
					tbs.setTime(gpstime);
					tbs.setCn(Integer.parseInt(db.getCn()));
					if(db.getCn().equals("1")||db.getCn().equals("2")) {
						tbs.setTableName("tbs_sbwy");
					}else {
						tbs.setTableName("tbs_sbwy_other");
					}
					tbsMapper.insertTbsTable(tbs);
				}else {
					throw new RuntimeException("传感器数目与数据长度不符合");
				}
			}
		}
		/** 更新接收数据时间 */
		tabOnlineMapper.updateOnline(zlBoSbwy.getCid(),DateUtil.formatTime());
	}
	/**
	 * @date:2020-9-2
	 * @param jsonParam 甲虫数据
	 * @return 解析甲虫小型化设备数据
	 **/
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public String parseJc(JcBo jc){
		try {
			if(jc.getData_flag()==0) {
				jc.setTime(DateUtil.formatTime());
			}
			String sql="INSERT INTO `tbs_lf`(`sn`,`acc_x_cur`,`acc_y_cur`,`acc_z_cur`,`tilt_x_init`,`tilt_y_init`,"
					+"`tilt_z_init`,`tilt_x_cur`,`tilt_y_cur`,`tilt_z_cur`,`wire_init`,`wire_cur`,`power`,`voltage`,`data_flag`,`time`)VALUES"+ 
					"('"+jc.getSn()+"','"+jc.getAcc_x_cur()+"','"+jc.getAcc_y_cur()+"','"+jc.getAcc_z_cur()+"','"+jc.getTilt_x_init()+
					"','"+jc.getTilt_y_init()+"','"+jc.getTilt_z_init()+"','"+jc.getTilt_x_cur()+"','"+jc.getTilt_y_cur()
					+"','"+jc.getTilt_z_cur()+"','"+jc.getWire_init()+"','"+jc.getWire_cur()+"','"+jc.getPower()+"','"+jc.getVoltage()
					+"','"+jc.getData_flag()+"','"+jc.getTime()+"');";
			jdbcTemplate.execute(sql);
			/** 更新接收数据时间 */
			tabOnlineMapper.updateOnline(jc.getSn(),DateUtil.formatTime());
			return "接收数据成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "接收数据失败";
		}
	}
    
}